package Utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 数据库连接管理类
 * @author Administrator
 *
 */
public final class DBUtil {

    public Connection conn;
    private PreparedStatement pstmt;
    public ResultSet rs;

    private  String driver_class;
    private  String url;
    private  String username;
    private  String password;

    public DBUtil(){
        //从配置文件中读取信息
        initConfig();
    }

    private void initConfig() {
        try {
            InputStream inputStream = getClass().getResourceAsStream("db.properties");
            //创建Properties类的实例
            Properties props = new Properties();
            //加载流对象
            props.load(inputStream);
            //读取内容
            driver_class = props.getProperty("driver");
            url = props.getProperty("url");
            username = props.getProperty("username");
            password = props.getProperty("password");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接对象
     * @return 数据库连接对象
     */
    public Connection getConnection(){
        try {
            Class.forName(driver_class);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 执行更新、删除、插入操作
     * @param sql	SQL语句
     * @param params  SQL语句中参数值
     * @return 受影响行数
     */
    public int executeUpdate(String sql,Object[] params){
        int rows = 0;
        conn = getConnection();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            if(params!=null){
                for(int i=0;i<params.length;i++){
                    pstmt.setObject(i+1, params[i]);
                }
            }
            rows = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            close(pstmt);
            close(conn);
        }
        return rows;
    }
    /**
     * 执行查询操作
     * @param sql	SQL语句
     * @param params  SQL语句中参数值
     * @return 结果集对象
     */
    public ResultSet executeQuery(String sql,Object[] params){
        ResultSet rs = null;
        conn = getConnection();
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            if(params!=null){
                for(int i=0;i<params.length;i++){
                    pstmt.setObject(i+1, params[i]);
                }
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 关闭数据库连接
     */
    public void close(Connection conn){
        try {
            if(conn!=null){
                conn.close();
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**
     * 关闭Statement对象
     * @param stmt
     */
    public void close(Statement stmt){
        try {
            if(stmt!=null){
                stmt.close();
                stmt = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void close(ResultSet rs){
        try {
            if(rs!=null){
                rs.close();
                rs = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

//	public static void main(String[] args) {
//		DBUtils db = new DBUtils();
//		db.conn = db.getConnection();
//		System.out.println("连接数据库成功！");
//	}
}
